﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：流程线条设置	
 */
var layerId = request('layerId');
var companyId = request('companyId');
var conditions = [];

var acceptClick;
var bootstrap = function ($, learun) {
    "use strict";
    var currentLine = top[layerId].currentLine;


    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {
            $('#lr_form_tabs').lrFormTab();
            $('#wftype').lrselect({// 1.是 2.否 6.是否 --3.超时 4.超时或是 5超时或否 预留
                placeholder: false,
                data: [{ id: '1', text: '是' }, { id: '2', text: '否' }, { id: '6', text: '正常流转' }]
            }).lrselectSet('1');

            // 条件节点设置
            $('#dbConditionId').lrselect({
                url: config.base_server() + 'dbLinks/tree?access_token=' + config.getToken().access_token,
                type: 'tree',
                placeholder: '请选择数据库',
                allowSearch: true
            });
            // 条件节点字段条件设置
            $('#condition_girdtable').jfGrid({
                headData: [
                    {label: "字段名称", name: "fieldName", width: 180, align: "left"},
                    {label: "字段ID", name: "fieldId", width: 180, align: "left"},
                    {
                        label: "编辑", name: "compareType", width: 80, align: "center",
                        formatter: function (cellvalue, row) {
                            switch (cellvalue)// 比较类型1.等于2.不等于3.大于4.大于等于5.小于6.小于等于7.包含8.不包含9.包含于10.不包含于
                            {
                                case '1':
                                    return '等于';
                                    break;
                                case '2':
                                    return '不等于';
                                    break;
                                case '3':
                                    return '大于';
                                    break;
                                case '4':
                                    return '大于等于';
                                    break;
                                case '5':
                                    return '小于';
                                    break;
                                case '6':
                                    return '小于等于';
                                    break;
                                case '7':
                                    return '包含';
                                    break;
                                case '8':
                                    return '不包含';
                                    break;
                                case '9':
                                    return '包含于';
                                    break;
                                case '10':
                                    return '不包含于';
                                    break;
                            }
                        }
                    },
                    {label: "数据值", name: "value", width: 200, align: "left"}
                ]
            });
            $('#lr_add_condition').on('click', function () {
                learun.layerForm({
                    id: 'AuthorizeForm',
                    title: '添加条件字段',
                    url: config.webportal_server + 'pages/workflow/wfscheme/conditionFieldForm.html?companyId='+companyId+'&access_token=' + config.getToken().access_token,
                    width: 400,
                    height: 300,
                    callBack: function (id) {
                        return top[id].acceptClick(function (data) {
                            data.id = learun.newGuid();
                            conditions.push(data);
                            $('#condition_girdtable').jfGridSet('refreshdata', conditions);
                        });
                    }
                });
            });
            $('#lr_edit_condition').on('click', function () {
                var _id = $('#condition_girdtable').jfGridValue('id');
                if (learun.checkrow(_id)) {
                    learun.layerForm({
                        id: 'AuthorizeForm',
                        title: '编辑条件字段',
                        url: config.webportal_server + 'pages/workflow/wfscheme/conditionFieldForm.html?companyId='+companyId+'&id=' + _id + '&&access_token=' + config.getToken().access_token,
                        width: 400,
                        height: 300,
                        callBack: function (id) {
                            return top[id].acceptClick(function (data) {
                                for (var i = 0, l = conditions.length; i < l; i++) {
                                    if (conditions[i].id == _id) {
                                        conditions[i] = data;
                                        $('#condition_girdtable').jfGridSet('refreshdata', conditions);
                                        break;
                                    }
                                }
                            });
                        }
                    });
                }
            });
            $('#lr_delete_condition').on('click', function () {
                var _id = $('#condition_girdtable').jfGridValue('id');
                if (learun.checkrow(_id)) {
                    learun.layerConfirm('是否确认删除该条件字段！', function (res, index) {
                        if (res) {
                            for (var i = 0, l = conditions.length; i < l; i++) {
                                if (conditions[i].id == _id) {
                                    conditions.splice(i, 1);
                                    $('#condition_girdtable').jfGridSet('refreshdata', conditions);
                                    break;
                                }
                            }
                            top.layer.close(index); //再执行关闭
                        }
                    });
                }
            });
        },
        initData: function () {
            $('#baseInfo').lrSetFormData(currentLine);
            if (!!currentLine.conditions) {
                conditions = currentLine.conditions;
            }
            $('#condition_girdtable').jfGridSet('refreshdata', conditions);
            $('#dbConditionId').lrselectSet(currentLine.dbConditionId);
            $('#conditionSql').val(currentLine.conditionSql || '');
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var formData = $('#baseInfo').lrGetFormData();
        currentLine.name = formData.name;
        currentLine.wftype = formData.wftype;

        currentLine.conditions = conditions;
        currentLine.dbConditionId = $('#dbConditionId').lrselectGet();
        currentLine.conditionSql = $('#conditionSql').val();

        callBack();
        return true;
    };
    page.init();
}